package com.pyb.java.commonutil.activity;

import android.app.Activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ActivityManager {

    private static final String TAG = "Activity";
    private static final List<Activity> ACTIVITYS = Collections.synchronizedList(new ArrayList<>());

    public static void create(Activity activity) {
        ACTIVITYS.add(activity);
    }

    public static void destroy(Activity activity) {
        ACTIVITYS.remove(activity);
    }

    public static void finishAll() {
        for (Activity activity : ACTIVITYS) {
            activity.finish();
        }
    }

    public static void finishOther(Activity currentActivity) {
        for (Activity activity : ACTIVITYS) {
            if (activity != currentActivity) {
                activity.finish();
            }
        }
    }

    public static void finishActivity(Activity activity) {
        for (Activity activity1 : ACTIVITYS) {
            if (activity == activity1) {
                activity.finish();
            }
        }
    }

    public static void finishActivityByClassName(String className) {
        for (Activity activity : ACTIVITYS) {
            if (activity.getClass().getName().equals(className)) {
                activity.finish();
            }
        }
    }

    public static Activity last() {
        int size = ACTIVITYS.size();
        if (size >= 1) {
            return ACTIVITYS.get(size - 1);
        }
        return null;
    }

    public static boolean hasActivity(Class tClass){
        for (Activity activity : ACTIVITYS) {
            if (tClass == activity.getClass()){
                return true;
            }
        }

        return false;
    }

    public static int size() {
        return ACTIVITYS.size();
    }

/**
 * 退出应用程序
 */
    /**
     * 应用程序退出
     */
    public static void appExit() {
        try {
            finishAll();
        } catch (Exception e) {
            Runtime.getRuntime().exit(-1);
        }
    }

}
